import { NextResponse } from 'next/server'
import prisma from '@/lib/prisma'
import {getUser} from '@/lib/authUtils'

export async function GET(request: Request): Promise<Response> {
  try {
    const userid = await getUser(request)
    if (!userid) {
      return new Response(JSON.stringify({ error: '未授权或登录信息过期，请登录' }), {
        status: 401,
        headers: {
          'Content-Type': 'application/json'
        }
      })
    }
    if (userid instanceof NextResponse) {
      return userid
    }

    const topics = await prisma.vms_topic.findMany({
      where: {
        user_id: userid
      },
      orderBy: {
        cjsj: 'desc'
      }
    })
    
    return new Response(JSON.stringify(topics), {
      headers: {
        'Content-Type': 'application/json'
      }
    })
  } catch (error: any) {
    return new Response(JSON.stringify({ error: error.message || '获取话题失败' }), {
      status: 500,
      headers: {
        'Content-Type': 'application/json'
      }
    })
  }
}

export async function POST(request: Request): Promise<Response> {
  try {
    const userid = await getUser(request)
    if (!userid) {
      return new Response(JSON.stringify({ error: '未授权或登录信息过期，请登录' }), {
        status: 401,
        headers: {
          'Content-Type': 'application/json'
        }
      })
    }
    if (userid instanceof NextResponse) {
      return userid
    }
    await prisma.vms_user.update({
      where: { id: userid },
      data: {
        topic_count: {
          increment: 1
        }
      }
    })
    
    const body = await request.json()
    const newTopic = await prisma.vms_topic.create({
      data: {
        ...body,
        id: crypto.randomUUID(),
        user_id: userid
      }
    })
    return new Response(JSON.stringify(newTopic), {
      status: 201,
      headers: {
        'Content-Type': 'application/json'
      }
    })
  } catch (error: any) {
    return new Response(JSON.stringify({ error: error.message || '创建话题失败' }), {
      status: 500,
      headers: {
        'Content-Type': 'application/json'
      }
    })
  }
}

export async function PUT(request: Request): Promise<Response> {
  try {
    const userid = await getUser(request)
    if (!userid || userid instanceof NextResponse) {
      return new Response(JSON.stringify({ error: '未授权或登录信息过期，请登录' }), {
        status: 401,
        headers: {
          'Content-Type': 'application/json'
        }
      })
    }

    const body = await request.json()
    console.log(userid)
    console.log(body.user_id)
    
    if (body.user_id !== userid) {
      return new Response(JSON.stringify({ error: '无权修改他人话题' }), {
        status: 403,
        headers: {
          'Content-Type': 'application/json'
        }
      })
    }
    const updatedTopic = await prisma.vms_topic.update({
      where: { id: body.id },
      data: body
    })
    return new Response(JSON.stringify(updatedTopic), {
      headers: {
        'Content-Type': 'application/json'
      }
    })
  } catch (error: any) {
    return new Response(JSON.stringify({ error: error.message || '更新话题失败' }), {
      status: 500,
      headers: {
        'Content-Type': 'application/json'
      }
    })
  }
}
